package app.project.directionlensproject.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.ArrayAdapter;
import app.project.directionlensproject.R;
import app.project.directionlensproject.fragment.FgListPlace;

public class DialogUtils {

	public static DialogUtils instance;

	public static DialogUtils getInstance() {
		if (instance == null) {
			instance = new DialogUtils();
		}

		return instance;
	}

	public static void showQuest(Context mContext, int title, int message, int yes, int no,
			DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {

		String yesString = mContext.getString(yes);
		String noString = mContext.getString(no);
		String titlelString;
		if (title == 0)
			titlelString = null;
		else
			titlelString = mContext.getString(title);
		String messageString = mContext.getString(message);

		showQuest(mContext, titlelString, messageString, yesString, noString, positive, negative);
	}

	public static void showQuest(Context mContext, String title, String message, String yes, String no,
			DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if (title != null)
			builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(yes, positive);
		builder.setNegativeButton(no, negative);
		builder.setCancelable(true).show();
	}

	public static void showInfoDialog(Context mContext, String title, String message,
			DialogInterface.OnClickListener positive) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if (title != null)
			builder.setTitle(title);

		builder.setMessage(message);
		builder.setPositiveButton("OK", positive);
		builder.setCancelable(true).show();
	}

	public static void showInfoDialog(Context mContext, int titleID, int mesageID,
			DialogInterface.OnClickListener positive) {
		String title = null;
		if (titleID != -1)
			title = mContext.getString(titleID);
		String message = mContext.getString(mesageID);
		showInfoDialog(mContext, title, message, positive);
	}

	public static void showDialogUser(Context mContext, DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);
		builderSingle.setTitle(mContext.getString(R.string.select_function));
		final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext,
				android.R.layout.select_dialog_item);
		arrayAdapter.add(mContext.getString(R.string.my_place));
		arrayAdapter.add(mContext.getString(R.string.edit_account));
		arrayAdapter.add(mContext.getString(R.string.logout));
		builderSingle.setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builderSingle.setAdapter(arrayAdapter, listener);
		builderSingle.show();
	}

	public static void showPlaceMenuContext(Context mContext, int type, DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);
		builderSingle.setTitle(mContext.getString(R.string.select_function));
		final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext,
				android.R.layout.select_dialog_item);
		if (type == FgListPlace.TYPE_SYTEM || type == FgListPlace.TYPE_APPROVE) {
			String tokenid = CommonUtils.getStringSetting(mContext, Constants.TOKEN_ID, null);
			if (tokenid != null)
				arrayAdapter.add(mContext.getString(R.string.add_to_favorite));
		} else if (type == FgListPlace.TYPE_UNAPPROVE) {
			arrayAdapter.add(mContext.getString(R.string.delete_place));
			arrayAdapter.add(mContext.getString(R.string.edit_place));
			arrayAdapter.add(mContext.getString(R.string.add_to_favorite));
		} else if (type == FgListPlace.TYPE_FAVORITE) {
			arrayAdapter.add(mContext.getString(R.string.delete_from_favorite));
		} else if (type == FgListPlace.TYPE_GOOGLE_SEARCH) {
			if (CommonUtils.getStringSetting(mContext, Constants.TOKEN_ID, null) != null)
				arrayAdapter.add(mContext.getString(R.string.add_to_my_place));
		}
		builderSingle.setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builderSingle.setAdapter(arrayAdapter, listener);
		builderSingle.show();
	}
}
